<?php

namespace app\wachat\controller;
use think\Controller;
use think\Lang;
use think\Request;
class Index extends Controller
{

    protected function _initialize()
    {
        $this->datas['status'] = '1';
        $this->datas['lang'] = 'zh-cn';
        cookie('think_var','zh-cn');
    }

    public function index()
    {

        //案例
        $ucase = cache('ucase' . cookie('think_var'));
        if (empty($ucase)) {
            $ucase = db('ucase')->alias('a')
                ->join('__NAV__ c', 'c.id= a.cid AND c.lang= a.lang AND c.status = a.status')
                ->field('a.*,c.name')
                ->where('a.status', '1')
                ->where('a.lang', cookie('think_var'))
                ->order('a.sort DESC,a.id DESC')
                ->limit(6)
                ->select();
            cache('ucase' . cookie('think_var'), $ucase);
        }

        //推荐产品
        $product= cache('product' . cookie('think_var'));
        if (empty($product)) {
            $product = db('product')
                ->alias('a')
                ->join('__NAV__ c', 'c.id= a.cid AND c.lang= a.lang AND c.status = a.status')
                ->field('a.image,a.id,a.title,a.cid,c.name')
                ->where('a.status', '1')
                ->where('a.tuijian', '1')
                ->where('a.lang', cookie('think_var'))
                ->order('a.sort DESC,a.id DESC')
                ->limit(6)
                ->select();

            cache('product' . cookie('think_var'), $product);
        }
//        dump($product);exit();

        //新闻动态
        $Article = cache('Article' . cookie('think_var'));
        if (empty($Article)) {
            $Article = db('Article')
                ->alias('a')
                ->join('__NAV__ c', 'c.id= a.cid AND c.lang= a.lang AND c.status = a.status')
                ->field('a.id,a.miaoshu,a.title,a.image,a.cid,c.name,c.create_time')
                ->where('a.status', '1')
                ->where('a.lang', cookie('think_var'))
                ->order('a.sort DESC,a.id DESC')
                ->limit(5)
                ->select();
            cache('Article' . cookie('think_var'), $Article);
        }
        $banner= cache('banner' . cookie('think_var'));
        if (empty($banner)) {
            $this->datas['model'] = 'mobile';
            $banner = model('carousel')
                ->where($this->datas)
                ->order('sort DESC')
                ->limit(5)
                ->select();
            cache('banner' . cookie('think_var'), $banner);
        }
//dump($Article);
        $resulr = ['data'=>[
            'product' => $product,
            'ucase' => $ucase,
            'article' => $Article,
            'category' => $product,
            'banner'=>$banner,
            'config'=>cache('systemsystem' . cookie('think_var')),
            ]
        ];
        echo json_encode($resulr);

    }

    public function waht(){
        $config = cache('systemsystem' . cookie('think_var'));
        $config['image'] ='public/uploads/image/5abca6143ca0f.png';
        $resulr = ['data'=>[
          'config'=>$config,
        ]
        ];
        echo json_encode($resulr);
    }

    public function lang()
    {
        switch ($this->request->param('lang')) {
            case 'en':
                cookie('think_var', 'en-us');
                break;
            case 'cn':
            default:
                cookie('think_var', 'zh-cn');
                break;
            //其它语言
        }
//        $this->redirect($this->request->server('HTTP_REFERER'));
    }
}